package com.evolved.automata.filetools;
import java.io.*;
import java.util.*;
public class StandardTools {

	public static void WriteTraceLog(String tracefilefullname,String message)
	{
		GregorianCalendar now = new GregorianCalendar();
		String logMessage=String.format(
				"[%1$s/%2$s/%3$s]%4$s:%5$s:%6$s  %7$s\r\n",
				now.get(Calendar.MONTH)+1,
				now.get(Calendar.DAY_OF_MONTH),
				now.get(Calendar.YEAR),
				now.get(Calendar.HOUR),
				now.get(Calendar.MINUTE),
				now.get(Calendar.SECOND),
				message);
		AppendAllText(tracefilefullname,logMessage);
	}
	public static void AppendAllText(String inputFileFullName, String text) throws RuntimeException
	{
		//input.spl
		BufferedWriter bWriter=null;
		
		try
		{
			bWriter = new BufferedWriter(new FileWriter(inputFileFullName,true));
			bWriter.write(text);
			
		}
		catch (Exception e)
		{
			
		}
		finally
		{
			if (bWriter!=null)
				try {
					bWriter.close();
				} catch (IOException e) {
					throw new RuntimeException(e.toString());
				}
		}
		
	}
	
	public static Boolean FileExists(String fileFullName)
	{
		File f = new File(fileFullName);
		return f.exists();
	}
	
	public static void DeleteFile(String fileFullName) throws RuntimeException
	{
		File f = new File(fileFullName);
		try
		{
			f.delete();
		}
		catch (Exception e)
		{
			java.io.StringWriter traceText = new StringWriter();
			java.io.PrintWriter pWriter = new PrintWriter(traceText,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(traceText.toString());
		}
	}
	
	public static void ImportCSVFile(String csvFileFullName, CSVRowImporter importer, boolean includeCellPositionP)
	{
		String inputFileFullName=csvFileFullName;
		BufferedReader bfReader=null;
		String lineInput="";
		String[] lineSplit;
		int i=0;
		try
		{
			bfReader = new BufferedReader(new FileReader(inputFileFullName));
			while ((lineInput=bfReader.readLine())!=null)
			{
				lineSplit=lineInput.split(",");
				
				for (int j=0;j<lineSplit.length;j++)
				{
					if (includeCellPositionP)
						importer.ImportCellData(lineSplit[j].trim(),i,j);
					else
						importer.ImportCellData(lineSplit[j].trim());
				}
				i++;
			}
		}
		catch (Exception e)
		{
			java.io.StringWriter traceText = new StringWriter();
			java.io.PrintWriter pWriter = new PrintWriter(traceText,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(traceText.toString());
		}
	}
	
	public static void ProcessFile(String inputFileFullName, LineInputProcessor lineProcessor) 
	{
		
		BufferedReader bfReader=null;
		String lineInput="";
		
		try
		{
			bfReader = new BufferedReader(new FileReader(inputFileFullName));
			while ((lineInput=bfReader.readLine())!=null)
			{
				lineProcessor.ProcessLine(lineInput);
			}
		}
		catch (Exception e)
		{
			java.io.StringWriter traceText = new StringWriter();
			java.io.PrintWriter pWriter = new PrintWriter(traceText,true);
			e.printStackTrace(pWriter);
			pWriter.close();
			throw new RuntimeException(traceText.toString());
		}
	}
	
}
